package cn.jichangxiu.ucenter.controller;

import cn.jichangxiu.common.ucenter.entity.Authority;
import cn.jichangxiu.common.ucenter.entity.User;
import cn.jichangxiu.common.web.model.response.QueryResult;
import cn.jichangxiu.common.web.model.response.R;
import cn.jichangxiu.common.web.model.response.ResponseCode;
import cn.jichangxiu.ucenter.service.inte.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "getUserByUserId")
    @ApiResponses(value = {@ApiResponse(code = 801 , message = "查询成功！") , @ApiResponse(code = 901 , message = "查询失败！")})
    @GetMapping("/getUserByUserId/{userId}")
    public R getUserByUserId(@PathVariable("userId") String userId){
        User user = userService.getUserByUserId(userId);
        if (user == null){
            return new R(ResponseCode.QUERY_FALL);
        }
        return new R(ResponseCode.QUERY_SUCCESS, new QueryResult(user));
    }

    @GetMapping("/getUserByAccount/{account}")
    public R getUserByAccount(@PathVariable("account") String account){
        User user = userService.getUserByAccount(account);
        if (user == null){
            return new R(ResponseCode.QUERY_FALL);
        }
        return new R(ResponseCode.QUERY_SUCCESS, new QueryResult(user));
    }

    @GetMapping("/getUserAuths/{userId}")
    public R getUserAuths(@PathVariable("userId") String userId){
        User user = userService.getUserByUserId(userId);
        if (user == null){
            return new R(ResponseCode.QUERY_FALL);
        }
        List<Authority> auths = userService.getUserAuths(user);
        return new R(ResponseCode.SUCCESS , new QueryResult(auths));
    }

}
